package com.jy.bo.common.conf.util;

/**
 * Created with IntelliJ IDEA.
 * User: binary
 * Date: 2019/1/31
 * Time: 下午3:46
 */
public class TextFormatUtil {

    /**
     * 格式化Key
     */
    public static String format(String formatKey, String... keyValues) {
        if (keyValues == null || keyValues.length == 0) {
            return formatKey;
        }
        StringBuilder key = new StringBuilder();
        char[] chars = formatKey.toCharArray();
        int index = -1;
        boolean inmark = false;
        boolean firstinmark = false;
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (ch == '{') {
                index++;
                inmark = true;
                firstinmark = true;
            } else if (inmark) {
                if (firstinmark) {
                    firstinmark = false;
                    key.append(keyValues[index]);
                }
                //判断下是否右括号
                if (ch == '}') {
                    inmark = false;
                }
            } else if (ch == '}') {
                inmark = false;
            } else {
                key.append(chars[i]);
            }
        }
        return key.toString();
    }
}
